<?php

namespace V1\Model;

use Think\Exception;
use Think\Model;

class MemberFansModel extends Model
{
    protected $tableName = 'member_fans';

    /**
     * 获取用户粉丝数量
     * @param $userid
     * @return mixed
     */
    public function get_fans_number($userid)
    {
        $data = $this->where(array('attention_uid' => $userid))->count();
        return $data;
    }

    /**
     * 获取我的粉丝列表
     * @param $userid [用户id]
     * @param null $type [类型]
     * @return array
     */
    public function get_fans_list($userid, $type = 0)
    {
        $data = $this->where(array('attention_uid' => $userid))->select();
        $member = new MemberProfileModel();
        $d = array();
        foreach ($data AS $k => $v) {
            $attention_detail = $member->get_user_details_common($v['fans_uid'], 'fans', $userid);//获取用户详细信息
            $data[$k]['fans_detail'] = $attention_detail;
            $data[$k]['fans_detail']['truename'] = $attention_detail['profile'][0]['member_profile']['truename'];
            $data[$k]['fans_detail']['avatar'] = $attention_detail['profile'][0]['member_profile']['avatar'];
            $data[$k]['fans_detail']['is_expert'] = $attention_detail['profile'][0]['is_expert'];
            if (isset($data[$k]['fans_detail']['is_expert']) == 1) {
                $data[$k]['fans_detail']['good_at_crop'] = $attention_detail['profile'][0]['expert_profile']['good_at_crop'];
            }
            unset($data[$k]['fans_detail']['profile']);
            if ($data[$k]['fans_detail']['is_expert'] != $type) {
                unset($data[$k]);
            }
        }
        foreach ($data as $k => $v) {
            $d[] = $v;
        }
        return $d;
    }

    /**
     * 获取用户关注数量
     * @param $userid
     * @return mixed
     */
    public function get_attention_number($userid)
    {
        $data = $this->where(array('fans_uid' => $userid))->count();
        return $data;
    }

    /**
     * 获取我的关注列表
     * @param $userid
     * @param null $type
     * @return mixed
     */
    public function get_attention_list($userid, $type = null)
    {
        $data = $this->where(array('fans_uid' => $userid))->select();
        $member = new MemberProfileModel();
        $d = array();
        foreach ($data AS $k => $v) {
            $attention_detail = $member->get_user_details_ask_list($v['attention_uid'], 'follow', $userid);//获取用户详细信息
            $data[$k]['attention_detail'] = $attention_detail;
            $data[$k]['attention_detail']['is_expert'] = $attention_detail['profile'][0]['is_expert'];
            if (isset($attention_detail['profile'][0]['is_expert'])) {
                unset($data[$k]['attention_detail']['profile'][0]['is_expert']);
            }
            if (isset($data[$k]['attention_detail']['is_expert'])) {
                if ($data[$k]['attention_detail']['is_expert'] != $type || $data[$k]['attention_detail'] == "") {
                    unset($data[$k]);
                }
            }
        }
        foreach ($data as $k => $v) {
            $d[] = $v;
        }
        return $d;
    }

    /**
     * 获取是否关注某人
     * @param string $attention_uid 关注人id
     * @param string $fans_uid 个人id
     * @return mixed
     * @throws Exception
     */
    public function is_follow($attention_uid, $fans_uid)
    {
        $map['attention_uid'] = $attention_uid;
        $map['fans_uid'] = $fans_uid;
        $count = D('member_fans')->where($map)->count();
        if (false === $count) {
            throw new Exception();
        } else {
            if ($count > 0) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    /**
     * 取消关注某人
     * @param string $uid 关注的用户id
     * @param string $fans_uid 自己的id
     * @return mixed
     * @throws Exception
     */
    public function del_attention($uid, $fans_uid)
    {
        $map['attention_uid'] = $uid;
        $map['fans_uid'] = $fans_uid;
        $request = $this->where($map)->delete();  // 删除主键为$id的参数
        if (false === $request) {
            throw new Exception();
        } else {
            return $request;
        }
    }
}
